<?php
    include_once('Models/CanBo.php');
    include_once('Models/NguoiDung.php');
    include_once('Models/CanBoDaoTao.php');
    include_once('Models/CanBoGiangDay.php');
    include_once('Models/NguoiQuanTri.php');
  if ($_SERVER['REQUEST_METHOD'] == 'POST'){ 
    $maso = trim($_POST['txtmaso']);
    $matkhau = md5(trim($_POST['txtmaso']));
    $holot = trim($_POST['txtholot']);
    $ten = trim($_POST['txtten']);
    $ngaysinh = trim($_POST['txtngaysinh']);
    $phai = $_POST['cbophai'];
    $sodt = trim($_POST['txtsodt']);
    $diachi = trim($_POST['txtdiachi']);
    $email = trim($_POST['txtemail']);
    $hocvi = $_POST['cbohocvi'];
    $chucvu = $_POST['cbochucvu'];
    $chucdanh = $_POST['cbochucdanh'];
    $loaicb = $_POST['cboloaicb'];
    $nghiepvu = trim($_POST['txtnghiepvu']);
    $mabm = $_POST['cbobomon'];
	$makhoa = $_POST['cbokhoa'];
    if ($loaicb == 'AD'){
            $maquyen = 'AD';
    }
    if ($loaicb == 'DT'){
        $maquyen = 'DT';
    }
    if ($loaicb == 'GV'){
        if ($chucvu == 'DV')
        {
            $maquyen = 'DV';
        }
        else if ($chucvu == 'BM'){
            $maquyen = 'BM';
        }
        else $maquyen = 'GV';
    }
    $cb = new CanBo();
    $nd = new NguoiDung();
    $cbdt = new CanBoDaoTao();
    $cbgd = new CanBoGiangDay();
    $nqt = new NguoiQuanTri();
    $nd->setMaSo($maso);
    $nd->setMatKhau($matkhau);
    $nd->setMaQuyen($maquyen);
    $cb->setMaSo($maso);
    $cb->setHoLot($holot);
    $cb->setTen($ten);
    $cb->setNgaySinh($ngaysinh);
    $cb->setPhai($phai);
    $cb->setSoDT($sodt);
    $cb->setEmail($email);
    $cb->setDiaChi($diachi);
    $cb->setHocVi($hocvi);
    //echo "Nguoi dung: ".$nd->getMaSo()."-".$nd->getMatKhau()."-".$nd->getMaQuyen();
    //echo "<br/>";
    //echo "Can bo: ".$cb->getMaSo()."-".$cb->getHoLot()."-".$cb->getTen()."-".$cb->getNgaySinh()."-".$cb->getPhai()."-".$cb->getEmail()."-".$cb->getDiaChi()."-".$cb->getSoDT()."-".$cb->getHocVi();
    if (isset($_POST['btnthem'])){
        if ($nd->isExits("NguoiDung","maso='".$nd->getMaSo()."'") > 0){
             echo "<script language=javascript> alert('Cán bộ đã tồn tại!'); </script>";
             require('Views/Capnhatcanbo.php');
        }
        else{
            mysql_query("START TRANSACTION");
            $i = 0;
            if ($nd->themNguoiDung() > 0){
                if ($cb->themCanBo() > 0){
                    if ($loaicb == 'AD'){
                        $nqt->setMaSo($maso);
                        $nqt->setNghiepvu($nghiepvu);
                        if ($nqt->themNguoiQuanTri() > 0)
                        {
                            mysql_query("COMMIT"); 
                            $i = 1;
                        } 
                    }
                    else if ($loaicb == 'DT'){
                        $cbdt->setMaSo($maso);
                        $cbdt->setNghiepvu($nghiepvu);
                        if ($cbdt->themCanBoDaoTao() > 0)
                        {
                            mysql_query("COMMIT"); 
                            $i = 1;
                        } 
                    }
                    else{
                        $cbgd->setMaSo($maso);
                        //echo $cbgd->getMaSo();
                        $cbgd->setMaBM($mabm);
                        $cbgd->setChucDanh($chucdanh);
                        if ($cbgd->themCanBoGiangDay() > 0)
                        {
                            mysql_query("COMMIT"); 
                            $i = 1;
                        } 
                        
                    }
                }
            }
            if ($i > 0){
                echo "<script language=javascript> alert('Đã thêm thành công!'); </script>";
                require('Views/Capnhatcanbo.php');
            }
            else{
                echo "<script language=javascript> alert('Thêm không được!'); </script>";
                require('Views/Capnhatcanbo.php');
            }
        }
		
    }
	else if (isset($_POST['btnsua'])){
			 if ($nd->isExits("NguoiDung","maso='".$nd->getMaSo()."'") <= 0){
				 echo "<script language=javascript> alert('Không tồn tại cán bộ này!'); </script>";
				 require('Views/Capnhatcanbo.php');
			}
			else{
				mysql_query("START TRANSACTION");
				$i = 0;
				if ($nd->suaNguoiDung() > 0){
					if ($cb->suaCanBo() > 0){
						//echo $cb->getQuery();
						if ($loaicb == 'AD'){
							$nqt->setMaSo($maso);
							$nqt->setNghiepvu($nghiepvu);
							if ($nqt->suaNguoiQuanTri() > 0)
							{
								//echo $nqt->getQuery();
								mysql_query("COMMIT"); 
								$i = 1;
							} 
						}
						else if ($loaicb == 'DT'){
							$cbdt->setMaSo($maso);
							$cbdt->setNghiepvu($nghiepvu);
							if ($cbdt->suaCanBoDaoTao() > 0)
							{
								mysql_query("COMMIT"); 
								$i = 1;
							} 
						}
						else{
							$cbgd->setMaSo($maso);
							//echo $cbgd->getMaSo();
							$cbgd->setMaBM($mabm);
							$cbgd->setChucDanh($chucdanh);
							if ($cbgd->suaCanBoGiangDay() > 0)
							{
								//echo $cbgd->getQuery();
								mysql_query("COMMIT"); 
								$i = 1;
							} 
							
						}
					}
				}
				if ($i >= 0){
					echo "<script language=javascript> alert('Đã sửa thành công!'); </script>";
					require('Views/Capnhatcanbo.php');
				}
				else{
					echo "<script language=javascript> alert('Sửa không được!'); </script>";
					require('Views/Capnhatcanbo.php');
				}
		}
	}
    else if (isset($_POST['btnxoa'])){
        if ($nd->isExits("NguoiDung","maso='".$nd->getMaSo()."'") <= 0){
             echo "<script language=javascript> alert('Cán bộ không tồn tại!'); </script>";
             require('Views/Capnhatcanbo.php');
        }
        else{
            mysql_query("START TRANSACTION");
            $i = 0;
            if ($loaicb == 'AD'){
                $nqt->setMaSo($maso);
                
                if ($nqt->xoaNguoiQuanTri() > 0)
                {
                    if ($cb->xoaCanBo() > 0){
                        if ($nd->xoaNguoiDung() > 0){
                            mysql_query("COMMIT"); 
                            $i = 1;
                        }
                    }
                    
                } 
            }
            else if ($loaicb == 'DT'){
                $cbdt->setMaSo($maso);
                
                if ($cbdt->xoaCanBoDaoTao() > 0)
                {
                    if ($cb->xoaCanBo() > 0){
                        if ($nd->xoaNguoiDung() > 0){
                            mysql_query("COMMIT"); 
                            $i = 1;
                        }
                    }
                    
                } 
            }
            else if ($loaicb == 'GV'){
                $cbgd->setMaSo($maso);
                if ($cbgd->isExits("LopHocPhan","maso='".$cbgd->getMaSo()."'") > 0){
                    echo "<script language=javascript> alert('Không thể xóa cán bộ này, muốn xóa xin vui lòng xóa hết các lớp học phần mà cán bộ này đã giảng dạy trước!'); </script>";
                    //require('Views/Capnhatcanbo.php');
                }
                else{
                    if ($cbgd->xoaCanBoGiangDay() > 0)
                    {
                        //echo $cbgd->getQuery();
                        if ($cb->xoaCanBo() > 0){
                            //echo $cb->getQuery();
                            if ($nd->xoaNguoiDung() > 0){
                                //echo $nd->getQuery();
                                mysql_query("COMMIT"); 
                                $i = 1;
                            }
                        }
                        
                    } 
                }
                
            }
            
            
            
            
            if ($i > 0){
                echo "<script language=javascript> alert('Đã xóa thành công!'); </script>";
                require('Views/Capnhatcanbo.php');
            }
            else{
                echo "<script language=javascript> alert('Xóa không được!'); </script>";
                require('Views/Capnhatcanbo.php');
            }
        }
		
    }
	else require('Views/Capnhatcanbo.php');
    
  }                                
  else include_once('Views/Capnhatcanbo.php');
?>
